/*给单选框或复选框添加样式*/
function checkbox($this){
    if($this.attr('type')=="checkbox"){
        if ($this.attr('checked')=="checked") {
            $this.removeAttr("checked");
            $this.next('img').attr("src","/static/images/c_checkbox_off.png");
        }else{
            $this.attr("checked","checked");
            $this.next('img').attr("src","/static/images/c_checkbox_on.png");
        }
    }
}

$(function(){
	//全部商品-弹窗
	$('#sample_allProd').on('click', function(){
		layer.open({
		  	type:1,
			title: '选择商品',
			area:['850px','700px'],
			btn:["确定","取消"],
            scrollbar: false,
			offset: "10px",
			//shadeClose:true,
			content:$('.j-mproChoose'),

            yes:function(){
		  	    console.log(allId)
                $.ajaxSetup({headers: {'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')}});
                $.ajax({
                    type: "POST",
                    url: "/dangerous/dangerous/dangerous_upstream",
                    dataType: 'json',
                    data: {allId: allId},
                    headers: {
                        'X-CSRF-TOKEN': $('meta[name="_token"]').attr('content')
                    },
                    success: function (data) {
                        console.log(data)
                        if(data.status==1){
                            var tr = $(".choosed_body").empty();
                            var addertr_up = $("#addertr_up").empty();
                            for (var i = 0; i < data.dataList.length; i++) {
                                var str = '<tr><td>'+data.dataList[i].goods_id+'</td><td>'+data.dataList[i].goods_name+'</td>';
                                var addertr_up_str = '<tr><td class="linhigh"><label class="lbmargin"><input type="checkbox" data-id='+data.dataList[i].goods_id+' class="checkson checkbox_child"/><img src="/static/images/c_checkbox_off.png/"/></label></td><td class="linhigh">'+i+'</td><td class="linhigh">'+data.dataList[i].goods_id+'</td><td class="linhigh">'+data.dataList[i].original_bn+'</td><td class="linhigh">'+data.dataList[i].goods_name+'</td><td class="linhigh">'+data.dataList[i].cas+'</td></tr>'
                                tr.append(str);
                                addertr_up.append(addertr_up_str);
                                console.log(str)
                            }

                            layer.msg('上游产品新增成功',{icon:1,time:1500,offset:"50%"});
                            closedHandler()


                        }else{
                            layer.msg(data.msg,{icon:2,time:1500,offset:"50%"});

                        }

                    }

                });

            },


		});
	});
	// //模糊查询demo-web
	// $("#j-subSeachBtn").on("click",function(){
	// 	var val = $.trim($(".j-serchput").val());
	// 	if(val != ""){
	// 		$("#serchTable tr:not('.hedtr')").hide().filter(":contains('"+val+"')").show();
	// 	}
	// })
//    下游商品添加
    $('#sampled_downLowerAdd').on('click', function(){
        layer.open({
            type:1,
            title: '选择商品',
            area:['850px','700px'],
            btn:["确定","取消"],
            scrollbar: false,
            offset: "10px",
            //shadeClose:true,
            content:$('.j-mproChoose'),

            yes:function(){
                console.log(allId)
                $.ajaxSetup({headers: {'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')}});
                $.ajax({
                    type: "POST",
                    url: "/dangerous/dangerous/dangerous_downstream",
                    dataType: 'json',
                    data: {allId: allId},
                    headers: {
                        'X-CSRF-TOKEN': $('meta[name="_token"]').attr('content')
                    },
                    success: function (data) {
                        console.log(data)
                        if(data.status==1){
                            var tr = $(".choosed_body").empty();
                            var addertr_down = $("#addertr_down").empty();
                            for (var i = 0; i < data.dataList.length; i++) {
                                var str = '<tr><td>'+data.dataList[i].goods_id+'</td><td>'+data.dataList[i].goods_name+'</td>';
                                var addertr_down_str = '<tr><td class="linhigh"><label class="lbmargin"><input type="checkbox" data-id='+data.dataList[i].goods_id+' class="checkson checkbox_child"/><img src="/static/images/c_checkbox_off.png/"/></label></td><td class="linhigh">'+i+'</td><td class="linhigh">'+data.dataList[i].goods_id+'</td><td class="linhigh">'+data.dataList[i].original_bn+'</td><td class="linhigh">'+data.dataList[i].goods_name+'</td><td class="linhigh">'+data.dataList[i].cas+'</td></tr>'
                                tr.append(str);
                                addertr_down.append(addertr_down_str);
                                console.log(str)
                            }

                            layer.msg('下游产品新增成功',{icon:1,time:1500,offset:"50%"});



                        }else{
                            layer.msg(data.msg,{icon:2,time:1500,offset:"50%"});
                        }

                    }

                });

            },


        });
    });
     
});

//tree结构
var setting = {
	view: {
		showLine: false,
		showIcon: false,
		selectedMulti: false,
		dblClickExpand: true, //双击
		addDiyDom: addDiyDom
	},
	data: {
		simpleData: {
			enable: true,
			idKey: "id",  //id
      		pIdKey: "pid", // fu id
      		
		}
	},
	callback: {
		onClick: zTreeOnClick
	}
};
var allId = []
$(function(){
    //全选
    $('.protCheckAll').click(function(){
        var $list_input=$(this).parents('.prot_head').next('.prot_body').find('input[type=checkbox]'); //找到子类的按钮
        var idAll =[];
        if ($(this).attr("checked")==undefined || $(this).attr("checked")=="") {
            $list_input.attr("checked","checked");
            $list_input.next('img').attr("src","/static/images/c_checkbox_on.png");
            $(".prot_body input[type='checkbox']").each(function() {
                var id = $(this).attr('data-id')
                idAll.push(id);
                allId=idAll
            })
            console.log(allId)
        }else{
            $list_input.removeAttr("checked");
            $list_input.next('img').attr("src","/static/images/c_checkbox_off.png");
            allId=[];

        }
        console.log(allId)
        checkbox($(this));
    })
    //单选
    $("body").on("change",".prot_body input[type='checkbox']",function(){

        var $ul_input=$(this).parents('.prot_body').prev('.tbhead').find('input');
        if($(this).parents('.tbhead').find("input[checked='checked']").length== $('.checkbox_child').length){
            $ul_input.prop("checked","checked");
            $ul_input.next('img').attr("src","/static/images/c_checkbox_on.png");
        }else{
            $ul_input.prop("checked",false);
            $ul_input.next('img').attr("src","/static/images/c_checkbox_off.png");
        }

        if($(this).attr("checked")=="" || $(this).attr("checked")==undefined){
            $(this).attr("checked",true);
            $(this).next('img').attr("src","/static/images/c_checkbox_on.png");
            var idAll =[];
            $(".prot_body input[type='checkbox']:checked").each(function(){
                var id = $(this).attr("data-id");
                console.log('正选')
                idAll.push(id);
                allId=idAll
            })

            console.log(allId)


        }else{
            $(this).attr("checked",false);
            $(this).next('img').attr("src","/static/images/c_checkbox_off.png");
            var id = $(this).attr("data-id");
            var index =  allId.indexOf(id);
            console.log(index)
            if(index>-1){
                allId.splice(index, 1);
            }else{
                allId=[]
            }
            console.log(allId)

        }
    })

})
//小图表箭头-展开
function addDiyDom(treeId, treeNode) {
	var spaceWidth = 5;
	var switchObj = $("#" + treeNode.tId + "_switch");
	icoObj = $("#" + treeNode.tId + "_ico");
	switchObj.remove();
	icoObj.before(switchObj);
	if (treeNode.level > 1) {
		//var spaceStr = "<span style='display: inline-block;width:" + (spaceWidth * treeNode.level)+ "px'></span>";
		var spaceStr = "<span style='display: inline-block;width:5px'></span>";
		switchObj.before(spaceStr);
	}
	
}
//数据读取 初始化
$.ajax({
  	url:"http://127.0.0.1:8020/html/zizhisystem/node.json",
  	type : "get", 
    dataType :"text",
  	success: function(data){

	  	var treeObj = $("#treeDemo");
		$.fn.zTree.init(treeObj, setting, arr);
	},
	error: function(){
		  
	}
});

//回调
function zTreeOnClick(event, treeId, treeNode) {
	var treeObj=$.fn.zTree.getZTreeObj("treeDemo"),
    nodes=treeObj.getSelectedNodes(true),  //获取光标选中的值(selected)
	v="";
    for(var i=0;i<nodes.length;i++){
     	v=nodes[i].name + ",";
    	console.log("节点的id:"+nodes[i].id+"节点的名称:"+nodes[i].name); //获取选中节点的值
	}
};
//关闭弹窗-layer
function closedHandler(){
    var index = parent.layer.getFrameIndex(window.name);

    //关闭弹出层
    parent.layer.close(index);
}

//上游原料checkbox
var goodsIndex = []
$(function(){
    //全选
    $("body").on("click",".upproAllImg",function(){
        var $list_input=$(this).parents('.tup_head').next('.tup_body').find('input[type=checkbox]'); //找到子类的按钮
        var idAll =[];
        if ($(this).attr("checked")==undefined || $(this).attr("checked")=="") {
            $list_input.attr("checked","checked");
            $list_input.next('img').attr("src","/static/images/c_checkbox_on.png");
            $(".tup_body input[type='checkbox']").each(function() {
                var id = $(this).attr('data-id')
                idAll.push(id);
                goodsIndex=idAll
            })
            console.log(goodsIndex)
        }else{
            $list_input.removeAttr("checked");
            $list_input.next('img').attr("src","/static/images/c_checkbox_off.png");
            goodsIndex=[];

        }
        console.log(goodsIndex)
        checkbox($(this));
    })

    $("body").on("click",".downLowerAllImg",function(){
        var $list_input=$(this).parents('.tdown_head').next('.tdown_body').find('input[type=checkbox]'); //找到子类的按钮
        var idAll =[];
        if ($(this).attr("checked")==undefined || $(this).attr("checked")=="") {
            $list_input.attr("checked","checked");
            $list_input.next('img').attr("src","/static/images/c_checkbox_on.png");
            $(".tdown_body input[type='checkbox']").each(function() {
                var id = $(this).attr('data-id')
                idAll.push(id);
                goodsIndex=idAll
            })
            console.log(goodsIndex)
        }else{
            $list_input.removeAttr("checked");
            $list_input.next('img').attr("src","/static/images/c_checkbox_off.png");
            goodsIndex=[];

        }
        console.log(goodsIndex)
        checkbox($(this));
    })
    //单选
    $("body").on("change",".tup_body input[type='checkbox']",function(){

        var $ul_input=$(this).parents('.tup_body').prev('.tup_head').find('input');
        if($(this).parents('.tup_head').find("input[checked='checked']").length== $('.checkbox_child').length){
            $ul_input.prop("checked","checked");
            $ul_input.next('img').attr("src","/static/images/c_checkbox_on.png");
        }else{
            $ul_input.prop("checked",false);
            $ul_input.next('img').attr("src","/static/images/c_checkbox_off.png");
        }

        if($(this).attr("checked")=="" || $(this).attr("checked")==undefined){
            $(this).attr("checked",true);
            $(this).next('img').attr("src","/static/images/c_checkbox_on.png");
            var idAll =[];
            $(".tup_body input[type='checkbox']:checked").each(function(){
                var id = $(this).attr("data-id");
                console.log('正选')
                idAll.push(id);
                goodsIndex=idAll
            })

            console.log(goodsIndex)


        }else{
            $(this).attr("checked",false);
            $(this).next('img').attr("src","/static/images/c_checkbox_off.png");
            var id = $(this).attr("data-id");
            var index =  goodsIndex.indexOf(id);
            console.log(index)
            if(index>-1){
                goodsIndex.splice(index, 1);
            }else{
                goodsIndex=[]
            }
            console.log(goodsIndex)

        }
    })
    $("body").on("change",".tdown_body input[type='checkbox']",function(){

        var $ul_input=$(this).parents('.tdown_body').prev('.tdown_head').find('input');
        if($(this).parents('.tdown_head').find("input[checked='checked']").length== $('.checkbox_child').length){
            $ul_input.prop("checked","checked");
            $ul_input.next('img').attr("src","/static/images/c_checkbox_on.png");
        }else{
            $ul_input.prop("checked",false);
            $ul_input.next('img').attr("src","/static/images/c_checkbox_off.png");
        }

        if($(this).attr("checked")=="" || $(this).attr("checked")==undefined){
            $(this).attr("checked",true);
            $(this).next('img').attr("src","/static/images/c_checkbox_on.png");
            var idAll =[];
            $(".tdown_body input[type='checkbox']:checked").each(function(){
                var id = $(this).attr("data-id");
                console.log('正选')
                idAll.push(id);
                goodsIndex=idAll
            })

            console.log(goodsIndex)


        }else{
            $(this).attr("checked",false);
            $(this).next('img').attr("src","/static/images/c_checkbox_off.png");
            var id = $(this).attr("data-id");
            var index =  goodsIndex.indexOf(id);
            console.log(index)
            if(index>-1){
                goodsIndex.splice(index, 1);
            }else{
                goodsIndex=[]
            }
            console.log(goodsIndex)

        }
    })
})
    //删除事件
$(function(){
        $("body").on("click","#sample_downLowerDel",function(){
        $(".tdwon_body input[type='checkbox']:checked").each(function(){
            alert( $(this).parents("tr").text());

        })
    })


        $("#sample_delpro").click(function(){

        $(".tup_body input[type='checkbox']:checked").each(function(){
           $(this).parents("tr").remove();


        })
    })


    })